//添加回复
export function addReplyComment(replyId, content) {
    let commentList = JSON.parse(localStorage.getItem("commentList"));
    addComment(commentList, replyId, content)
    localStorage.setItem("commentList", JSON.stringify(commentList));
}
//添加评论
export function addMomentComment(content, momentId) {
    //实际应该得根据评论的帖子id进行添加评论，这里没有帖子所以momentId没有使用
    let commentList = JSON.parse(localStorage.getItem("commentList"));
    let now = new Date();
    let commentId = Date.parse(now);
    commentList.push({
        id: commentId,
        name: "LWF",
        time: now.toLocaleString(),
        content: content,
        children: []
    })
    localStorage.setItem("commentList", JSON.stringify(commentList));
}
//通过所回复评论的id在其childern数组添加子评论
function addComment(commentList, replyId, content) {
    for (let i = 0; i < commentList.length; i++) {
        if (commentList[i].id === replyId) {
            let now = new Date();
            let commentId = Date.parse(now);
            commentList[i].children.push({
                id: commentId,
                name: "LWF",
                time: now.toLocaleString(),
                content: content,
                children: []
            })
            return
        } else {
            addComment(commentList[i].children, replyId, content)
        }
    }
}
//初始化localStorage里的数据,预先先添加一些评论
export function initLocalStorage() {
    if (localStorage.getItem("commentList")) return
    let commentList = [
        {
            id: 1,
            name: "Tom",
            time: "2021/10/11 下午12:04:17",
            content: "第一条评论",
            children: [
                {
                    id: 11,
                    name: "Mike",
                    time: "2021/10/12 下午12:04:17",
                    content: "第一条评论的评论",
                    children: [
                        {
                            id: 111,
                            name: "Jack",
                            time: "2021/10/13 下午12:04:17",
                            content: "第一条评论的评论的评论",
                            children: []
                        }
                    ]
                },
                {
                    id: 12,
                    name: "KkK",
                    time: "2021/10/14 下午12:04:17",
                    content: "第一条评论的评论",
                    children: []
                }
            ]
        },
        {
            id: 2,
            name: "Lucy",
            time: "2021/10/11 下午15:04:17",
            content: "第二条评论",
            children: []
        }
    ];
    localStorage.setItem("commentList", JSON.stringify(commentList));

}
